package com.travel.controller.user;

import com.travel.model.dto.comment.CommentRequest;
import com.travel.model.dto.common.ApiResponse;
import com.travel.model.dto.service.ServiceDTO;
import com.travel.model.entity.auth.User;
import com.travel.model.entity.content.Comment;
import com.travel.service.Service.ServiceService;
import com.travel.service.Service.TravelService;
import com.travel.service.Service.UserService;
import com.travel.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
//用户服务接口 , 获取服务列表, 创建服务, 更新服务, 删除服务, 添加评论, 删除评论
@Tag(name = "用户服务", description = "用户服务管理接口")
@RestController
@RequestMapping("/api/user/services")
@RequiredArgsConstructor
public class UserServiceController {
    
    private final TravelService travelService;
    private final UserService userService;
    private final ServiceService serviceService;
    
    @Operation(summary = "获取服务列表")
    @GetMapping
    public ApiResponse<List<ServiceDTO>> getUserServices() {
        try {
            User currentUser = userService.getCurrentUser();
            return ApiResponse.success("获取成功", travelService.getUserServices(currentUser.getId()));
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }
    
    @Operation(summary = "创建服务")
    @PostMapping
    public ApiResponse<ServiceDTO> createService(
            @Parameter(description = "服务信息", required = true)
            @Valid @RequestBody ServiceDTO serviceDTO) {
        try {
            ServiceDTO created = travelService.createService(serviceDTO);
            return ApiResponse.success("创建成功", created);
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }
    
    @Operation(summary = "更新服务")
    @PutMapping("/{id}")
    public ApiResponse<ServiceDTO> updateService(
            @Parameter(description = "服务ID", required = true) @PathVariable Long id,
            @Parameter(description = "服务信息", required = true)
            @Valid @RequestBody ServiceDTO serviceDTO) {
        try {
            ServiceDTO updated = travelService.updateService(id, serviceDTO);
            return ApiResponse.success("更新成功", updated);
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }
    
    @Operation(summary = "删除服务")
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteService(
            @Parameter(description = "服务ID", required = true) @PathVariable Long id) {
        try {
            travelService.deleteService(id);
            return ApiResponse.success("删除成功");
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }
    
    @Operation(summary = "添加评论", description = "为服务添加评论")
    @PostMapping("/{serviceId}/comments")
    public ApiResponse<Comment> addComment(
            @Parameter(description = "服务ID") @PathVariable Long serviceId,
            @Valid @RequestBody CommentRequest request) {
        Comment comment = serviceService.addComment(serviceId, request);
        return ApiResponse.success("评论成功", comment);
    }
    
    @Operation(summary = "删除评论", description = "删除服务评论")
    @DeleteMapping("/comments/{commentId}")
    public ApiResponse<Void> deleteComment(
            @Parameter(description = "评论ID") @PathVariable Long commentId) {
        serviceService.deleteComment(commentId, SecurityUtils.getCurrentUserId());
        return ApiResponse.success("删除成功");
    }
} 